home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 051-075 / scopedisk68 / calendar2 / calendar.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  15KB  |  519 lines

  1. /***************************************************************/
  2. /*                  Calendar Program                           */
  3. /*    This program opens a window, reads the system date and   */
  4. /*    displays the current month calendar and date.  The mouse */
  5. /*    may be used to change the date.  Saves date on Exit      */
  6. /***************************************************************/
  7.  
  8. #include <exec/types.h>
  9. #include <exec/devices.h>
  10. #include <exec/io.h>
  11. #include <exec/libraries.h>
  12. #include <devices/timer.h>
  13. #include <functions.h>
  14. #include <intuition/intuition.h>
  15. #include <graphics/gfxmacros.h>
  16. #include <stdio.h>
  17. #include <ctype.h>
  18. #include <time.h>
  19.  
  20. #define INTUITION_REV 33L
  21. #define GRAPHICS_REV  33L
  22. #define MAXLIN           80
  23.  
  24. char *mn[12] = {"January","February","March","April","May","June","July",
  25.         "August","September","October","November","December"};
  26. short nday[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  27. char *Curr_Year = "1984";
  28. char *Time = "12:00";
  29. char s[]="Date 12:00 07-Jul-86";
  30. short CC,CR,da,wk,mo,yr,hr,min,sec;
  31. USHORT pot[2];
  32. short matrix[6][7];
  33. static char *lines[6] = {"                    ",  /* init lines for calendar*/
  34.               "                    ",
  35.              "                    ",
  36.              "                    ",
  37.              "                    ",
  38.              "                    ",
  39.             };
  40.  
  41. struct IntuitionBase  *IntuitionBase;
  42. struct GfxBase           *GfxBase;
  43. struct Window         *Win;
  44. struct FileHandle     *Open();
  45. struct Gadget          Cg[8];
  46. struct PropInfo          Cinfo[2];
  47. struct Image          Cimage[2];
  48.  
  49. main(argc,argv)
  50. int  argc;
  51. char *argv[];
  52. {
  53.     struct IntuiMessage *message;
  54.     struct Gadget     *gad;  
  55.     ULONG  class,code, x,y;
  56.     BOOL   Quit,Save,Memo,GadFlag;
  57.     USHORT ID;
  58.     float  real;
  59.     
  60.     short dx,dy,wkdy,maxday;
  61.     VOID  show_month(),Last_Time(),show_day(),FrameIt(),Point_Block();
  62.     VOID  Reset_Date(),Show_Time(),Show_Help(),OpenAll(),Make_Prop_Gadget(),
  63.       Make_BOOL_Gadget(),Get_Hours(),Get_Minutes(),Tag(),Reminders(),
  64.       Set_Time();
  65.     short day_of_week(),set_days(),Get_Month();
  66.     long  Convert();
  67.  
  68.     Last_Time();OpenAll();FrameIt(Win);
  69.     maxday=set_days(mo+1,yr);wkdy=day_of_week(mo,yr);
  70.     show_month(da,mo,yr,maxday,wkdy);
  71.     Show_Time(0);Point_Block(CC,CR);
  72.     if (argc<2) OffGadget(&Cg[7],Win,NULL);
  73.  
  74.     for (Quit=FALSE,Save=TRUE,Memo=FALSE,GadFlag=FALSE;!Quit;) {
  75.        while (!(message=GetMsg(Win->UserPort))) WaitPort(Win->UserPort);
  76.        class = message->Class;    code = message->Code;
  77.        x     = message->MouseX;   y    = message->MouseY;
  78.     gad   = (struct Gadget *)message->IAddress;
  79.        ReplyMsg( message );
  80.        switch ( class ) {
  81.               case CLOSEWINDOW: Quit=TRUE;Save=FALSE;break;
  82.         case GADGETDOWN:  ID=gad->GadgetID;GadFlag=TRUE;
  83.                   if (ID>5) {
  84.                   Quit=TRUE;if (ID==7) Memo=TRUE;
  85.                 } else if (ID>1) {
  86.                       switch(ID) {
  87.                     case 2: yr--;break;
  88.                     case 3: yr++;break;
  89.                     case 4: mo--;if (mo<0) { yr--;mo=11; }
  90.                         if (da>nday[mo]) da=nday[mo];
  91.                         break;
  92.                     case 5: mo++;if (mo>11) { yr++;mo=0; }
  93.                         if (da>nday[mo]) da=nday[mo];
  94.                         break;
  95.                               }
  96.                            maxday=set_days(mo+1,yr);
  97.                                    wkdy=day_of_week(mo,yr);
  98.                                    Point_Block(CC,CR);
  99.                                    show_month(da,mo,yr,maxday,wkdy);
  100.                                    Point_Block(CC,CR);
  101.                     }
  102.                    break;
  103.               case MOUSEBUTTONS: if (code==SELECTDOWN && y>47 && y<121) {
  104.                 dx=(x-6)/24;dy=(y-50)/10;
  105.                 if (dx<0) dx=0;if (dx>6) dx=6;
  106.                 if (dy<0) dy=0;if (dy>5) dy=5;
  107.                 if (matrix[dy][dx]) {
  108.                     Point_Block(CC,CR);Point_Block(dx,dy);
  109.                 CC=dx;CR=dy;da=matrix[dy][dx];
  110.                   }
  111.             GadFlag=FALSE;
  112.                }
  113.              break;
  114.               case MOUSEMOVE:if (ID<2 && GadFlag && Cinfo[ID].HorizPot!=pot[ID]){
  115.             real=(Cinfo[ID].HorizPot+1.0)/65536.0;
  116.             if (ID==0) hr=real*24; else min=real*60;
  117.             pot[ID]=Cinfo[ID].HorizPot;Show_Time();
  118.          }
  119.             }
  120.       }
  121.     CloseWindow(Win);
  122.     if (yr>1978 && Save) {
  123.       Reset_Date();Set_Time(Convert());Tag();
  124.     if (Memo && argc==2) Reminders(argc,argv);
  125.       }
  126. }
  127.  
  128.  
  129. VOID Tag()        /* Write File with current date to S: */
  130. {
  131.     struct FileHandle   *out;
  132.  
  133.     if ((out=Open("S:now",1006L)) != NULL) {
  134.     Write(out,&s,(long)strlen(s));Close(out);
  135.       } else printf("Can't update S:now !!!\n");
  136. }
  137.  
  138. VOID Reminders(num,arg)        /* Display Reminders */
  139. int num;
  140. char *arg[];
  141. {
  142.     FILE   *in;
  143.     char   line[MAXLIN];
  144.     short  min,max,day,month,time,cnt,sw,a1,a2;
  145.  
  146.     if (da<6) min=(mo-1)*100+(nday[mo-1]+da-5);
  147.         else min=mo*100+da-5;
  148.     if ((da+10)>nday[mo]) max=(mo+1)*100+(da+10-nday[mo]);
  149.         else max=mo*100+da+10;
  150.     if (min<1) min=1;
  151.     if (max>1131) max=1131;sw=1;
  152.     if ((in=fopen(arg[1],"r")) != NULL) {
  153.         while (fgets(line,MAXLIN,in) != NULL) {
  154.         day=line[0]-48;
  155.         if (day<1) day=0;
  156.         if (line[1] != 32) {
  157.             day=day*10+line[1]-48;
  158.             cnt=3;
  159.           } else cnt=2;
  160.         month=Get_Month(line[cnt],line[cnt+1],line[cnt+2])-1;
  161.         time=month*100+day;
  162.         if ((time>=min) && (time<=max)) {
  163.             if (sw) {
  164.             printf("\n");
  165.             printf("%2d %-13sReminders\n",da,mn[mo]);
  166.             printf("-----------------------------------\n");
  167.             sw=0;
  168.               }
  169.             printf(line);
  170.           }
  171.           }
  172.         fclose(in);
  173.       } else printf("%s file not available...\n",arg[1]);
  174.     printf("\n");
  175. }
  176.  
  177. short Get_Month(a,b,c)      /* Convert Alpha Month to Number */
  178. short a,b,c;
  179. {
  180.     short val;
  181.  
  182.     if (a>96) a-=32;
  183.     if (b>96) b-=32;
  184.     if (c>96) c-=32;
  185.     switch (a) {
  186.     case 65: if (b==80) val=4; else val=8;
  187.          break;
  188.     case 68: val=12;break;
  189.     case 70: val=2;break;
  190.     case 74: if (b==65) val=1;
  191.              else if (c==78) val=6; else val=7;
  192.          break;
  193.     case 77: if (c==82) val=3; else val=5;
  194.          break;
  195.     case 78: val=11;break;
  196.     case 79: val=10;break;
  197.     case 83: val=9;break;
  198.     }
  199.     return val;
  200. }
  201.  
  202. VOID OpenAll()        /* Open everything */
  203. {
  204.     struct NewWindow nw;
  205.     USHORT i;
  206.     float  real;
  207.  
  208.     if (!(IntuitionBase=(struct IntuitionBase *)
  209.     OpenLibrary("intuition.library",INTUITION_REV))) {
  210.         printf("Intuition Open failed.\n");
  211.           exit(FALSE);
  212.           }
  213.     if (!(GfxBase=(struct GfxBase *)
  214.     OpenLibrary("graphics.library",GRAPHICS_REV))) {
  215.         printf ("graphics open failed.\n");
  216.         exit(FALSE);
  217.           }
  218.     nw.LeftEdge=232;        nw.TopEdge=12;            nw.Width=178;
  219.     nw.Height=144;        nw.DetailPen=0x00;      nw.BlockPen=0x01;
  220.     nw.Title=(UBYTE *)" Calendar ";
  221.     nw.Flags=ACTIVATE|SMART_REFRESH|WINDOWCLOSE|WINDOWDRAG|
  222.         WINDOWDEPTH|REPORTMOUSE;
  223.     nw.IDCMPFlags=CLOSEWINDOW|MOUSEBUTTONS|MOUSEMOVE|GADGETDOWN|GADGETUP;
  224.     nw.Type=WBENCHSCREEN;    nw.FirstGadget=&Cg[0];    nw.CheckMark=NULL;
  225.     nw.Screen=NULL;        nw.BitMap=NULL;         nw.MinWidth=100;
  226.     nw.MinHeight=60;        nw.MaxWidth=640;        nw.MaxHeight=200;
  227.  
  228.     real=(float)hr/24.0;pot[0]=real*65536+1;
  229.     real=(float)min/60.0;pot[1]=real*65536+1;
  230.  
  231.     Make_Prop_Gadget(&Cg[0],8,120,80,7,&Cinfo[0],&Cimage[0],pot[0]);
  232.     Cg[0].NextGadget=&Cg[1];
  233.     Make_Prop_Gadget(&Cg[1],90,120,80,7,&Cinfo[1],&Cimage[1],pot[1]);
  234.     Cg[1].NextGadget=&Cg[2];
  235.     Make_BOOL_Gadget(&Cg[2],9,14,48,8);Cg[2].NextGadget=&Cg[3];
  236.     Make_BOOL_Gadget(&Cg[3],120,14,48,8);Cg[3].NextGadget=&Cg[4];
  237.     Make_BOOL_Gadget(&Cg[4],9,24,48,8);Cg[4].NextGadget=&Cg[5];
  238.     Make_BOOL_Gadget(&Cg[5],120,24,48,8);Cg[5].NextGadget=&Cg[6];
  239.     Make_BOOL_Gadget(&Cg[6],9,130,76,8);Cg[6].NextGadget=&Cg[7];
  240.     Make_BOOL_Gadget(&Cg[7],93,130,76,8);
  241.     for (i=0;i<8;i++) Cg[i].GadgetID=i;
  242.     Win=(struct Window *)(OpenWindow(&nw));
  243. }
  244.  
  245. VOID Make_Prop_Gadget(g,l,t,w,h,info,image,pot)
  246. struct Gadget *g;
  247. USHORT l,t,w,h;
  248. USHORT pot;
  249. struct Image    *image;
  250. struct PropInfo *info;
  251. {
  252.     info->Flags=AUTOKNOB|FREEHORIZ;
  253.     info->VertPot=0L;        info->HorizPot=(ULONG)pot;
  254.     info->HorizBody=1L;        info->VertBody=1L;
  255.     g->NextGadget=NULL;            g->LeftEdge=l;         g->TopEdge=t;
  256.     g->Width=w;            g->Height=h;
  257.     g->Flags=GADGHCOMP|SELECTED;
  258.     g->Activation=GADGIMMEDIATE|FOLLOWMOUSE|RELVERIFY;
  259.     g->GadgetType=PROPGADGET;   g->GadgetRender=(APTR)image;
  260.     g->SelectRender=NULL;    g->GadgetText=NULL;  g->MutualExclude=NULL;
  261.     g->SpecialInfo=(APTR)info;  g->GadgetID=NULL;    g->UserData=NULL;
  262. }
  263.  
  264. VOID Make_BOOL_Gadget(g,l,t,w,h)
  265. struct Gadget *g;
  266. USHORT  l,t,w,h;
  267. {
  268.     g->NextGadget=NULL;            g->LeftEdge=l;         g->TopEdge=t;
  269.     g->Width=w;            g->Height=h;
  270.     g->Flags=GADGHCOMP;
  271.     g->Activation=GADGIMMEDIATE;
  272.     g->GadgetType=BOOLGADGET;   g->GadgetRender=NULL;
  273.     g->SelectRender=NULL;    g->GadgetText=NULL;  g->MutualExclude=NULL;
  274.     g->SpecialInfo=NULL;        g->GadgetID=NULL;    g->UserData=NULL;
  275. }
  276.  
  277. void Reset_Date()        /* Reset Date String to Current Date */
  278. {
  279.     hr %=24;
  280.     s[5]=(hr/10)+48;
  281.     s[6]=(hr%10)+48;
  282.     s[8]=(min/10)+48;
  283.     s[9]=(min%10)+48;
  284.     s[11]=(da/10)+48;
  285.     s[12]=(da%10)+48;
  286.     s[14]=mn[mo][0];
  287.     s[15]=mn[mo][1];
  288.     s[16]=mn[mo][2];
  289.     s[18]=((yr%100)/10)+48;
  290.     s[19]=(yr%10)+48;
  291. }
  292.  
  293. void Show_Time(x)        /* Display Time */
  294. short x;
  295. {
  296.   short h,i,morn;
  297.  
  298.     if (min>59) min=min-60;
  299.     if (min<0) min=60+min;
  300.     if (hr>24) hr=hr-24;
  301.     if (hr<1) hr=24+hr;
  302.     if (hr>12) h=hr-12; else h=hr;
  303.     Time[0]=(h/10)+48;
  304.     Time[1]=(h%10)+48;
  305.     Time[3]=(min/10)+48;
  306.     Time[4]=(min%10)+48;
  307.     Move(Win->RPort,69L,118L);
  308.     Text(Win->RPort,Time,5L);
  309.     Move(Win->RPort,118L,118L);
  310.     SetAPen(Win->RPort,3L); 
  311.     if ((hr>11) && (hr<24)) Text(Win->RPort,"PM",2L);
  312.       else Text(Win->RPort,"AM",2L);
  313.     SetAPen(Win->RPort,1L);   
  314. }
  315.  
  316. void Point_Block(nx,ny)        /* Draw Cursor Square around Day */
  317. SHORT nx,ny;
  318. {
  319.    LONG x,y;
  320.  
  321.     x=nx*24+7;y=ny*10+58;
  322.     SetDrMd(Win->RPort,COMPLEMENT);
  323.     Move(Win->RPort,x+1,y);
  324.     Draw(Win->RPort,x+19,y);
  325.     Draw(Win->RPort,x+19,y-10);
  326.     Draw(Win->RPort,x,y-10);
  327.     Draw(Win->RPort,x,y);
  328.     SetDrMd(Win->RPort,JAM2);
  329. }
  330.  
  331. void FrameIt(w)            /* Draw Frame for Calendar Window */
  332. struct Window *w;
  333. {   
  334.     short i;
  335.  
  336.     SetDrMd(Win->RPort,JAM2);
  337.     SetAPen(Win->RPort,3L);
  338.     Move(w->RPort,5L,12L);
  339.     Draw(w->RPort,172L,12L);
  340.     Draw(w->RPort,172L,141L);
  341.     Draw(w->RPort,5L,141L);
  342.     Draw(w->RPort,5L,12L);
  343.     Move(w->RPort,5L,35L);
  344.     Draw(w->RPort,172L,35L);
  345.     Move(w->RPort,5L,109L);
  346.     Draw(w->RPort,172L,109L);
  347.     Move(w->RPort,5L,128L);
  348.     Draw(w->RPort,172L,128L);
  349.     Move(w->RPort,89L,128L);
  350.     Draw(w->RPort,89L,141L);
  351.     SetAPen(Win->RPort,1L);
  352.     RectFill(Win->RPort,9L,130L,85L,139L);
  353.     RectFill(Win->RPort,93L,130L,168L,139L);
  354.     Move(Win->RPort,22L,118L);Text(Win->RPort,"Time",4L);
  355.     SetAPen(Win->RPort,2L);
  356.     Move(Win->RPort,9L,32L);
  357.     Text(Win->RPort," <<<<          >>>> ",20L);
  358.     Move(Win->RPort,9L,22L);
  359.     Text(Win->RPort," <<<<          >>>> ",20L);
  360.     SetBPen(Win->RPort,1L);
  361.     Move(Win->RPort,15L,137L);Text(Win->RPort,"Set Date",8L);
  362.     Move(Win->RPort,98L,137L);Text(Win->RPort,"Memo/Set",8L);
  363.     SetBPen(Win->RPort,0L);
  364. }
  365.  
  366. void Last_Time()        /* Read the System Date and Time */
  367. {
  368.  
  369.     struct tm *localtime(),*tstruct;
  370.     long   tloc,time(),t;
  371.     short  i;
  372.  
  373.     tloc=time();
  374.     tstruct=localtime(&tloc);
  375.     mo=tstruct->tm_mon;
  376.     yr=tstruct->tm_year+1900;
  377.     da=tstruct->tm_yday;
  378.     wk=tstruct->tm_wday;
  379.     hr=tstruct->tm_hour;
  380.     min=tstruct->tm_min;
  381.     sec=tstruct->tm_sec;
  382.     for (i=0;i<mo;i++) da -= nday[i];
  383.     if (da<1) {
  384.     mo-=1;da=nday[mo];
  385.       } 
  386. }
  387.  
  388. short day_of_week(mo,yr)    /* Calculate the Day of the Week */
  389. short mo,yr;
  390. {
  391.     float r;
  392.     short i,j,k,l;
  393.  
  394.     r=365.0*yr+1+31.0*mo;mo=mo+1;
  395.     if (mo>2) {
  396.         i=0.4*mo+2.3;j=yr/4;k=yr/100;l=0.75*(k+1);
  397.         r=r-i+j-l;
  398.     } else {
  399.         i=(yr-1)/4;j=(yr-1)/100;k=0.75*(j+1);
  400.         r=r+i-k;
  401.       }
  402.     r=r-273750.0;
  403.     j=(-1.0*r)/7.0;i=r+j*7.0;
  404.     if(i==7) i=0;
  405.     return i;
  406. }
  407.  
  408. void show_month(da,mo,yr,mxday,wkday)  /* Display Day, Month, Year */
  409. short da,mo,yr,mxday,wkday;
  410. {
  411.     short cnt,row,i,j,x,y;
  412.  
  413.     for (i=0;i<6;i++) {
  414.       for (j=0;j<7;j++) {
  415.         matrix[i][j]=0;
  416.         lines[i][j*3]=32;
  417.         lines[i][j*3+1]=32;
  418.        }
  419.      }
  420.         Move(Win->RPort,49L,32L);
  421.         Text(Win->RPort,"          ",10L);
  422.     SetAPen(Win->RPort,1L);
  423.     i=(178-strlen(mn[mo])*8)/2;
  424.     Curr_Year[0]=(yr/1000)+48;
  425.     Curr_Year[1]=((yr%1000)/100)+48;
  426.     Curr_Year[2]=((yr%100)/10)+48;
  427.     Curr_Year[3]=yr%10+48;
  428.     Move(Win->RPort,73L,22L);
  429.     Text(Win->RPort,Curr_Year,4L);
  430.     Move(Win->RPort,(long)i,32L);
  431.     Text(Win->RPort,mn[mo],(long)strlen(mn[mo]));
  432.     Move(Win->RPort,9L,46L);
  433.     SetAPen(Win->RPort,3L);
  434.     Text(Win->RPort," S  M  T  W  T  F  S",20L);
  435.     cnt=wkday;row=0;
  436.     for (i=1;i<(mxday+1);i++) {
  437.         if (i==da) {
  438.         CR=row;CC=cnt;
  439.         }
  440.         if (i>9) lines[row][cnt*3]=(i/10)+48;
  441.             else lines[row][cnt*3]=32;
  442.         lines[row][cnt*3+1]=(i%10)+48;
  443.         matrix[row][cnt]=i;    
  444.         cnt++;
  445.         if (cnt==7 && i<mxday) {
  446.             cnt=0;row++;
  447.         }
  448.     }
  449.     SetAPen(Win->RPort,1L);
  450.     for (i=0;i<6;i++) {
  451.         Move(Win->RPort,9L,(long)(i*10+56));
  452.         Text(Win->RPort,lines[i],20L);
  453.     }
  454. }
  455.  
  456. short set_days(mo,yr)    /* Check for Leap Year */
  457. short mo,yr;
  458. {
  459.    short norm;
  460.  
  461.     if (mo==1 || mo==3 || mo==5 || mo==7 || mo==8 || mo==10 || mo==12)
  462.         return 31;
  463.       else if (mo==4 || mo==6 || mo==9 || mo==11)
  464.           return 30;
  465.         else {
  466.           norm = 1;
  467.           if ((yr % 4) == 0) norm = 0;
  468.           if ((yr % 100) == 0) norm = 1;
  469.           if ((yr % 400) == 0) norm = 0;
  470.           if (norm) return 28;
  471.             else return 29;
  472.           }
  473. }
  474.  
  475. VOID  Set_Time(secs)     /* Set System Clock */
  476. long secs;
  477. {
  478.     struct timerequest *time_req;
  479.     struct timerequest *CreateExtIO();
  480.     struct MsgPort     *timer_port;
  481.     long   error;
  482.              
  483.     timer_port = CreatePort( 0L, 0L );
  484.     if ( ! timer_port ) exit( 1L );
  485.     time_req=CreateExtIO(timer_port,(long)sizeof(struct timerequest));
  486.     if (time_req==NULL) exit(1L);
  487.     error =  OpenDevice (TIMERNAME,UNIT_MICROHZ,time_req,0L);
  488.     if( error ) {
  489.         DeleteExtIO(time_req,(long)sizeof(struct timerequest));
  490.         DeletePort( timer_port );
  491.     exit( 2L );
  492.       }
  493.     time_req -> tr_node.io_Message . mn_ReplyPort = timer_port;
  494.     time_req -> tr_node.io_Command = TR_SETSYSTIME;
  495.     time_req -> tr_time.tv_secs    = secs;
  496.     time_req -> tr_time.tv_micro   = 0L;
  497.     DoIO( time_req );
  498.     CloseDevice( time_req );
  499.     DeleteExtIO(time_req,(long)sizeof(struct timerequest)); 
  500.     DeletePort( timer_port );
  501.  }
  502.  
  503. long Convert()        /* Convert Time to Seconds */
  504. {
  505.     long secs,years,days;
  506.     short i;
  507.  
  508.     years=yr-1978;
  509.     if (years<0) return(0L);
  510.     for (i=0,days=0;i<mo;i++) days+=nday[i];
  511.     days+=da-1;
  512.     if (yr>1979) {
  513.     days+=((yr-1980)/4)+1;
  514.     if (((yr-1980)%4)==0 && mo<2) days--;
  515.       }
  516.     secs=(years*365+days)*86400+(long)hr*3600+min*60+sec;
  517.     return(secs);
  518.